<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskModelConfig extends HelpDeskModel 
{
	/**
	 * constructor
	 * @return array Array of objects containing the data from the frequent
	 */	
	function __construct() {
		
	}

	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery() 
	{
		$database 	= &HelpDeskConfig::getDBO();
		$user 		= &HelpDeskConfig::getUser();
		$config 	= &HelpDeskConfig::getInstance();

		// publishedid
			$publishedid = $this->getField( $config->getFieldname( 'publishedid' ) );
			$publishedid_query = "";
			if (intval($publishedid) == '1') {
				$publishedid_query = " AND db.published = '" . trim( strtolower( $publishedid ) ) . "'";
			} elseif (isset($publishedid) && $publishedid == '0') {
				$publishedid_query = " AND db.published = '0'";
			}
			
		// search
			$where = array();
			$search	= $this->getField( $config->getFieldname( 'search' ) );
			if (isset($search)) 
			{
				$where[] = " LOWER(db.id) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
				$where[] = " LOWER(db.name) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
				$where[] = " LOWER(db.element) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
			}
			$where_query = count( $where ) ? "\n HAVING " . implode( ' OR ', $where ) : "";
						
		// order
			$order_query = "";
			$order = $this->getState( 'order' );
			$order_dir = $this->getState( 'order_dir' );
			switch ($order) 
			{
				case "order":
				case "ordering":
					$order_query = " ORDER BY db.ordering ".strtoupper( $order_dir )." ";
				  break;
				case "element":
					$order_query = " ORDER BY db.element ".strtoupper( $order_dir )." ";
				  break;
				case "name":
					$order_query = " ORDER BY db.name ".strtoupper( $order_dir )." ";
				  break;
				case "published":
					$order_query = " ORDER BY db.published ".strtoupper( $order_dir )." ";
				  break; 
				case "id":
				default:
					$order_query = " ORDER BY db.id ".strtoupper( $order_dir )." ";
				  break;
			}

		// build query
			$query = "
				SELECT 
					db.* 
				FROM 
					#__mambots as db
				WHERE 1
					AND LOWER(db.folder) = 'helpdesk'
					{$publishedid_query}
					{$where_query} 
				{$order_query}
			";

		return $query;
	}

	/**
	 * 
	 * @return unknown_type
	 */
	function save()
	{
		//		$mainframe 	= HelpDeskConfig::getApplication();
		$database 	= HelpDeskConfig::getDBO();
		//		$date 		= HelpDeskConfig::getDate();

		$success = true;
		$errorMsg = '';
		
		$config = &HelpDeskConfig::getInstance();
		$properties = $config->getProperties();
					
		if ($properties) { foreach ($properties as $key => $value ) {
			unset($row);
			$row = $this->getTable( 'config' );
			$newvalue = HelpDeskRequest::getVar( $key );
			$value_exists = array_key_exists( $key, $_POST );
			if ( $value_exists && !empty($key) ) 
			{ 
				// proceed if newvalue present in request. prevents overwriting for none existant values.
				$row->load( $key );
				$row->title = $key;
				$row->value = $newvalue;
				if (!$row->store() ) {
					$success = false;
					$errorMsg .= HelpDeskText::_( "Could not store")." $key - ";	
				}
			}
		} }
		
		$this->setError( $errorMsg );
		
		return $success;
		
	}
	
}